package com.league.zjxn.jetpack.vmodel

import android.app.Application
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import com.league.zjxn.app.SPSConstance
import com.league.zjxn.base.BaseViewModel
import com.league.zjxn.entity.*
import com.league.zjxn.jetpack.repository.AccessRepository
import com.league.zjxn.jetpack.repository.HomeRepository
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.SubscribeViewModel
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Flowable
import java.util.concurrent.TimeUnit


class AccessViewModel(application: Application) : BaseViewModel(application) {

    private val mHomeRepository: AccessRepository = AccessRepository()


    val mMapLoginPass: MutableMap<String, String> = mutableMapOf()

    val mMapLoginCode: MutableMap<String, String> = mutableMapOf()

    val mMapRegister: MutableMap<String, String> = mutableMapOf()

    val mMapUpdatePass: MutableMap<String, String> = mutableMapOf()

    val userInfoLiveData = MutableLiveData<HttpResponseBody<LoginResponse>>()
    fun userLoginPass() {
        setRxLifeScope(object : SubscribeViewModel<LoginResponse>(userInfoLiveData) {
            override fun onSuccess(resp: LoginResponse) {
                super.onSuccess(resp.apply {
                    SPSConstance.token = data.token
                    SPSConstance.userInfo = data.user
                })
            }
        }) {
            mHomeRepository.userLoginPass(mMapLoginPass)
        }
    }

    fun userLoginSms() {
        mMapLoginCode["ticket"] = sendSmsCodeLiveData.value?.data?.data?.ticket ?: return
        setRxLifeScope(object : SubscribeViewModel<LoginResponse>(userInfoLiveData) {
            override fun onSuccess(resp: LoginResponse) {
                super.onSuccess(resp.apply {
                    SPSConstance.token = data.token
                    SPSConstance.userInfo = data.user
                })
            }
        }) {
            mHomeRepository.userLoginSms(mMapLoginCode)
        }
    }

    val userUpdatePassLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userUpdatePass() {
        mMapUpdatePass["ticket"] = sendSmsCodeLiveData.value?.data?.data?.ticket ?: return
        setRxLifeScope(SubscribeViewModel(userUpdatePassLiveData)) {
            mHomeRepository.userUpdatePass(mMapUpdatePass)
        }
    }

    val userRegisterLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userRegister() {
        mMapRegister["ticket"] = sendSmsCodeLiveData.value?.data?.data?.ticket ?: return
        setRxLifeScope(object : SubscribeViewModel<HttpResponseData>(userRegisterLiveData) {
            override fun onSuccess(resp: HttpResponseData) {
                super.onSuccess(resp)
                mMapLoginPass.clear()
                mMapLoginPass["mobile"] = (mMapRegister["mobile"] ?: return)
                mMapLoginPass["password"] = (mMapRegister["password"] ?: return)
                userLoginPass()
            }
        }) {
            mHomeRepository.userRegister(mMapRegister)
        }
    }


    val sendSmsCodeLiveData = MutableLiveData<HttpResponseBody<SendSmsCodeResponse>>()
    fun userSendSmsCodeRegister(textView: TextView) {
        val mobile = mMapRegister["mobile"] ?: return
        setRxLifeScope(object : SubscribeViewModel<SendSmsCodeResponse>(sendSmsCodeLiveData) {
            override fun onSuccess(resp: SendSmsCodeResponse) {
                super.onSuccess(resp)
                startCodeDownTime(textView)
            }
        }) {
            mHomeRepository.userSendSmsCode(mobile, "register")
        }
    }

    fun userSendSmsCodeForget(textView: TextView) {
        val mobile = mMapUpdatePass["mobile"] ?: return
        setRxLifeScope(object : SubscribeViewModel<SendSmsCodeResponse>(sendSmsCodeLiveData) {
            override fun onSuccess(resp: SendSmsCodeResponse) {
                super.onSuccess(resp)
                startCodeDownTime(textView)
            }
        }) {
            mHomeRepository.userSendSmsCode(mobile, "password")
        }
    }

    fun userSendSmsCodeLogin(textView: TextView? = null) {
        val mobile = mMapLoginCode["mobile"] ?: mMapSettingPhone["mobile"] ?: return
        setRxLifeScope(object : SubscribeViewModel<SendSmsCodeResponse>(sendSmsCodeLiveData) {
            override fun onSuccess(resp: SendSmsCodeResponse) {
                super.onSuccess(resp)
                textView?.let {
                    startCodeDownTime(it)
                }
            }
        }) {
            mHomeRepository.userSendSmsCode(mobile, "default")
        }
    }


    val mMapSettingPass: MutableMap<String, String> = mutableMapOf()


    fun userSettingPwd() {
        setRxLifeScope(object : SubscribeViewModel<LoginResponse>(userInfoLiveData) {
            override fun onSuccess(resp: LoginResponse) {
                super.onSuccess(resp.apply {
                    SPSConstance.token = data.token
                    SPSConstance.userInfo = data.user
                })
            }
        }) {
            mHomeRepository.userSettingPwd(mMapSettingPass)
        }
    }


    val mMapSettingPhone: MutableMap<String, String> = mutableMapOf()

    fun userSettingPhone() {
        mMapSettingPhone["ticket"] = sendSmsCodeLiveData.value?.data?.data?.ticket ?: return
        setRxLifeScope(object : SubscribeViewModel<LoginResponse>(userInfoLiveData) {
            override fun onSuccess(resp: LoginResponse) {
                super.onSuccess(resp.apply {
                    SPSConstance.token = data.token
                    SPSConstance.userInfo = data.user
                })
            }
        }) {
            mHomeRepository.userSettingPhone(mMapSettingPhone)
        }
    }

    /**
     * 获取验证码倒计时
     * */
    fun startCodeDownTime(textView: TextView) {
        val disposable = Flowable.intervalRange((1), (60), (0), (1), TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext {
                textView.text = "${60 - it}s"
            }
            .doOnSubscribe {
                textView.isEnabled = false
            }
            .doOnComplete {
                textView.text = "验证码"
                textView.isEnabled = true
            }
            .subscribe()
        dispose.add(disposable)
    }
}